package com.huanxing.cloud.upms.admin.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanxing.cloud.common.core.entity.Result;
import com.huanxing.cloud.common.log.annotation.SysLog;
import com.huanxing.cloud.security.annotation.HxInner;
import com.huanxing.cloud.security.entity.HuanxingUser;
import com.huanxing.cloud.security.utils.SecurityUtils;
import com.huanxing.cloud.upms.admin.service.ISysRoleService;
import com.huanxing.cloud.upms.admin.service.ISysUserService;
import com.huanxing.cloud.upms.common.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

/**
 * 用户管理
 *
 * @author lijx
 * @since 2022/2/26 16:45
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
@Api(value = "user", tags = "用户管理")
public class SysUserController {
  private final ISysUserService sysUserService;
  private final ISysRoleService sysRoleService;
  private static final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

  @HxInner
  @ApiOperation(value = "获取指定用户全部信息")
  @GetMapping("/info/{username}")
  public Result getUserInfo(@PathVariable String username) {
    SysUser sysUser = sysUserService.findUserByName(username);

    if (ObjectUtil.isNull(sysUser)) {
      return Result.fail(String.format("用户信息为空 %s", username));
    }
    return Result.success(sysUserService.findUserInfo(sysUser));
  }

  @ApiOperation(value = "获取当前用户全部信息")
  @GetMapping("/info")
  public Result info() {
    HuanxingUser huanxingUser = SecurityUtils.getUser();
    SysUser sysUser = sysUserService.getById(huanxingUser.getUserId());
    if (ObjectUtil.isNull(sysUser)) {
      return Result.fail("用户信息为空");
    }
    return Result.success(sysUserService.findUserInfo(sysUser));
  }

  @ApiOperation(value = "用户列表")
  @PreAuthorize("hasAuthority('upms:sysuser:page')")
  @GetMapping("/page")
  public Result page(Page page, SysUser sysUser) {
    return Result.success(sysUserService.adminPage(page, sysUser));
  }

  @ApiOperation(value = "通过id查询用户信息")
  @PreAuthorize("hasAuthority('upms:sysuser:get')")
  @GetMapping("/{id}")
  public Result<SysUser> getById(@PathVariable String id) {
    SysUser sysUser = sysUserService.getById(id);
    if (ObjectUtil.isNull(sysUser)) {
      return Result.fail("用户不存在");
    }
    sysUser.setRoles(ArrayUtil.toArray(sysRoleService.findRoleIdsByUserId(id), String.class));
    return Result.success(sysUser);
  }

  @ApiOperation(value = "用户新增")
  @SysLog("新增用户")
  @PreAuthorize("hasAuthority('upms:sysuser:add')")
  @PostMapping
  public Result add(@RequestBody SysUser sysUser) {
    if (ArrayUtil.isEmpty(sysUser.getRoles())) {
      return Result.fail("角色不能为空");
    }
    sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
    return Result.success(sysUserService.saveUser(sysUser));
  }

  @ApiOperation(value = "用户修改")
  @SysLog("修改用户")
  @PreAuthorize("hasAuthority('upms:sysuser:edit')")
  @PutMapping
  public Result edit(@RequestBody SysUser sysUser) {
    sysUser.setPassword(null);
    sysUser.setPhone(null);
    if (ArrayUtil.isEmpty(sysUser.getRoles())) {
      return Result.fail("角色不能为空");
    }
    return Result.success(sysUserService.updateUser(sysUser));
  }
}
